//
//  WBStatusCell.swift
//  SinaWeibo_Swift
//
//  Created by TianZhen on 16/4/14.
//  Copyright © 2016年 TianZhen. All rights reserved.
//

import UIKit
import SnapKit

let WBStatusCellMargin: CGFloat = 10

class WBStatusCell: UITableViewCell {
    
    /// 记录之前bottomView的top约束
    var bottomViewTopCons: Constraint?
    
    // 微博模型
    var status: WBStatus? {
        didSet {
            // 当控制器给cell设置模型后,来做操作
            // 将微博模型传递给originalView
            originalView.status = status
            
            // 先卸载掉之前的bottomView的顶部约束
            bottomViewTopCons?.uninstall()
            // 如果有被转发的微博,将被转发的微博设置给被转发的view
            if let retweetStatus = status?.retweeted_status {
                retweetView.hidden = false
                retweetView.retweetStatus = retweetStatus
                
                // 有被转发微博的时候bottomView的顶部参照被转发view的底部
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                // 如果没有被转发的微博,将被转发的view隐藏
                // 虽然retweetView被隐藏了但是bottomView的顶部约束还是会参照被转发的view
                retweetView.hidden = true
                
                // 将bottomView的顶部约束参照原创view的底部
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 设置cell背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        // 2.添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.right.equalTo(contentView)
            
            // 顶部有10得间距
            make.top.equalTo(contentView).offset(10)
            
            // 子控件的高度随机
            // 高度参照子控件的高度
            //            make.height.equalTo(Int(arc4random_uniform(200)) + 50)
        }
        
        // 被转发的view
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            
            // TODO: 测试
            //            make.height.equalTo(200)
            // 实际上被转发view的高度需要参考自己被转发的配图
        }
        
        // bottomView
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(37)
            
            // bottomView的顶部参照被转发view的底部
            bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        
        // cell的高度根据里面子控件的约束来自动调整
        // 3.cell的子控件约束要正确,还需要约束contentView的约束(最主要的是高度约束),系统会让cell取contentView的高度
        contentView.snp_makeConstraints { (make) -> Void in
            // contentView的左右上参照cell
            make.leading.top.trailing.equalTo(self)
            
            // contentView的高度约束不能参照cell,需要参照子控件
            make.bottom.equalTo(bottomView)
        }
    }
    
    // MARK: - 懒加载
    /// 原创的view
    private lazy var originalView: WBStatusOriginalView = WBStatusOriginalView()
    
    /// 底部的view
    private lazy var bottomView: WBStatusBottomView = WBStatusBottomView()
    
    /// 被转发的view
    private lazy var retweetView: WBStatusRetweetView = WBStatusRetweetView()
}
